package cz.muni.fi.pa165.dundd.assembler;

import cz.muni.fi.pa165.dundd.dto.AdministratorDTO;
import cz.muni.fi.pa165.dundd.dto.HeroDTO;
import cz.muni.fi.pa165.dundd.dto.HeroRoleDTO;
import cz.muni.fi.pa165.dundd.dto.TroopDTO;
import cz.muni.fi.pa165.dundd.entity.Administrator;
import cz.muni.fi.pa165.dundd.entity.Hero;
import cz.muni.fi.pa165.dundd.entity.HeroRole;
import cz.muni.fi.pa165.dundd.entity.Troop;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;

/**
 *
 * @author xszanto1 <ardos@mail.muni.cz>, xdemocko
 */
@Component
public class Assembler {

    public static Administrator administratorDtoToEntity(AdministratorDTO adminDTO) {
        if (adminDTO == null) {
            throw new IllegalArgumentException();
        }
        Administrator admin = new Administrator();
        admin.setId(adminDTO.getId());
        admin.setDescription(adminDTO.getDescription());
        admin.setName(adminDTO.getName());
        admin.setPassword(adminDTO.getPassword());

        return admin;
    }

    public static AdministratorDTO administratorEntityToDTO(Administrator admin) {
        if (admin == null) {
            throw new IllegalArgumentException();
        }
        AdministratorDTO adminDTO = new AdministratorDTO();
        adminDTO.setId(admin.getId());
        adminDTO.setDescription(admin.getDescription());
        adminDTO.setName(admin.getName());
        adminDTO.setPassword(admin.getPassword());

        return adminDTO;
    }

    public static HeroRole heroRoleDtoToEntity(HeroRoleDTO roleDTO) {
        if (roleDTO == null) {
            throw new IllegalArgumentException();
        }
        HeroRole role = new HeroRole();
        role.setId(roleDTO.getId());
        role.setDescription(roleDTO.getDescription());
        role.setName(roleDTO.getName());

        return role;
    }

    public static HeroRoleDTO heroRoleEntityToDTO(HeroRole role) {
        if (role == null) {
            throw new IllegalArgumentException();
        }
        HeroRoleDTO roleDTO = new HeroRoleDTO();
        roleDTO.setId(role.getId());
        roleDTO.setDescription(role.getDescription());
        roleDTO.setName(role.getName());

        return roleDTO;
    }

    public static Hero heroDtoToEntity(HeroDTO heroDTO) {
        if (heroDTO == null) {
            throw new IllegalArgumentException();
        }
        Hero hero = new Hero();

        hero.setId(heroDTO.getId());
        hero.setXp(heroDTO.getXp());
        hero.setName(heroDTO.getName());
        hero.setPassword(heroDTO.getPassword());


        TroopDTO troopDTO = heroDTO.getMyParty();
        if (troopDTO != null) {
            hero.setMyParty(troopDtoToEntity(troopDTO));
        }
  
        List<HeroRoleDTO> roleIDs = heroDTO.getRoles();
        List<HeroRole> roles = new ArrayList<>();
        if (roleIDs != null) {
            for (HeroRoleDTO heroRoleID : roleIDs) {
                roles.add(heroRoleDtoToEntity(heroRoleID));
            }   
        }
        hero.setRoles(roles);
        return hero;
    }

    public static HeroDTO heroEntityToDTO(Hero hero) {
        if (hero == null) {
            throw new IllegalArgumentException();
        }
        HeroDTO heroDTO = new HeroDTO();
        heroDTO.setId(hero.getId());
        heroDTO.setName(hero.getName());
        heroDTO.setXp(hero.getXp());
        heroDTO.setPassword(hero.getPassword());

        Troop troop = hero.getMyParty();
        if (troop != null) {
            heroDTO.setMyParty(troopEntityToDTO(troop));
        }

        List<HeroRole> roles = hero.getRoles();
        List<HeroRoleDTO> rolesIDs = new ArrayList<>();
        if (roles != null && roles.size() > 0) {
            for (HeroRole heroRole : roles) {
                rolesIDs.add(heroRoleEntityToDTO(heroRole));
            }
        }
        heroDTO.setRoles(rolesIDs);
        return heroDTO;
    }

    public static Troop troopDtoToEntity(TroopDTO troopDTO) {
        if (troopDTO == null) {
            throw new IllegalArgumentException();
        }
        Troop troop = new Troop();
        troop.setId(troopDTO.getId());
        troop.setGold(troopDTO.getGold());
        troop.setMission(troopDTO.getMission());
        troop.setName(troopDTO.getName());

        return troop;
    }

    public static TroopDTO troopEntityToDTO(Troop troop) {
        if (troop == null) {
            throw new IllegalArgumentException();
        }
        TroopDTO troopDTO = new TroopDTO();
        troopDTO.setId(troop.getId());
        troopDTO.setGold(troop.getGold());
        troopDTO.setMission(troop.getMission());
        troopDTO.setName(troop.getName());
        return troopDTO;
    }

}
